# ./gui/gui.py -- Main gui design file.


# Import Needed
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import *

import sys, typing, inspect

from constants import * 
from utils.nlp.reply import get_reply


# Main UI
class UI(QWidget):
    def __init__(self, loggerset, parent: typing.Optional['QWidget'] = None) -> None:
        super().__init__(parent)
        self.loggerset = loggerset
        self.initUI() # Inition

    def initUI(self):
        # Window
        self.setWindowTitle("Kristina") # Title
        self.grid = QGridLayout() # Grid layout
        self.vlayout = []
        self.name = []
        self.text = []
        self.i = 0
        # Kristina Test
        self.vlayout.append(QVBoxLayout())
        self.name.append(QLabel("Kristina"))
        self.name[0].setFont(QtGui.QFont("KaiTi", 16, QtGui.QFont.Bold)) # Set Name
        self.vlayout[0].addWidget(self.name[0])
        self.text.append(QLabel("Hi, master! "))
        self.text[0].setFont(QtGui.QFont("KaiTi", 14))
        self.vlayout[0].addWidget(self.text[0])
        self.grid.addLayout(self.vlayout[0], 0, 0)
        self.i += 1
        # Master Test
        self.vlayout.append(QVBoxLayout())
        self.name.append(QLabel("You"))
        self.name[1].setFont(QtGui.QFont("KaiTi", 16, QtGui.QFont.Bold)) # Set Name
        self.vlayout[1].addWidget(self.name[1])
        self.text.append(QLabel("Hi ~ "))
        self.text[1].setFont(QtGui.QFont("KaiTi", 14))
        self.vlayout[1].addWidget(self.text[1])
        self.grid.addLayout(self.vlayout[1], 1, 4)
        self.i += 1
        # Input Box
        self.entry = QTextEdit()
        self.entry.setFixedWidth(500)
        self.entry.setFixedHeight(50)
        self.grid.addWidget(self.entry, 300, 0, 1, 0, QtCore.Qt.AlignCenter)
        # Submit Button
        self.submit_btn = QPushButton("Submit")
        self.submit_btn.clicked.connect(self.submit)
        self.grid.addWidget(self.submit_btn, 301, 4, QtCore.Qt.AlignRight)
        # Main Layout
        self.setLayout(self.grid)

    def submit(self):
        answer = self.entry.toPlainText()
        self.show_message(answer, "You")
        reply_text, reply_mode = get_reply(self.loggerset, answer, "normal", self.show_message)
        self.show_message(reply_text)
        if reply_mode == "&quit" or reply_text == "&quit":    # For quit
            self.loggerset.log("Quiting Main Loop", ln=inspect.getframeinfo(
                inspect.currentframe()).lineno, filename=__file__)
            sys.exit()
    
    def show_message(self, text:str, who="Kristina"):
        if who == "You":
            self.vlayout.append(QVBoxLayout())
            self.name.append(QLabel("You"))
            self.name[self.i].setFont(QtGui.QFont("KaiTi", 16, QtGui.QFont.Bold))  # Set Name
            self.vlayout[self.i].addWidget(self.name[self.i])
            self.text.append(QLabel(text))
            self.text[self.i].setFont(QtGui.QFont("KaiTi", 14))
            self.vlayout[self.i].addWidget(self.text[self.i])
            self.grid.addLayout(self.vlayout[self.i], self.i, 4)
            self.i += 1
        elif who == "Kristina":
            self.vlayout.append(QVBoxLayout())
            self.name.append(QLabel("Kristina"))
            self.name[self.i].setFont(QtGui.QFont(
                "KaiTi", 16, QtGui.QFont.Bold))  # Set Name
            self.vlayout[self.i].addWidget(self.name[self.i])
            self.text.append(QLabel(text))
            self.text[self.i].setFont(QtGui.QFont("KaiTi", 14))
            self.vlayout[self.i].addWidget(self.text[self.i])
            self.grid.addLayout(self.vlayout[self.i], self.i, 0)
            self.i += 1
        return self.entry.toPlainText()


def startgui(loggerset):
    loggerset.log("Gui opening", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    app = QApplication(sys.argv)
    ui = UI(loggerset)
    ui.show()
    loggerset.log("Gui quiting", ln=inspect.getframeinfo(inspect.currentframe()).lineno, filename=__file__)
    sys.exit(app.exec_())